package com.icewld.dus;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.IBinder;
import android.view.WindowManager;

public class HDCAService extends Service {

    private static WindowManager mWindowManager;
    private static MonitorView mMonitorView;

    public HDCAService() {
    }

    @Override
    public void onCreate(){
        super.onCreate();
        showWindow();
    }

    @Override
    public void onDestroy(){
        hideWindow();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private void showWindow() {
        if (mWindowManager == null) {
            mWindowManager = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
            mMonitorView = new MonitorView(HDCAService.this);
        }
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                1, 1 , //Must be at least 1x1
                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                //Don't know if this is a safe default
                PixelFormat.TRANSLUCENT);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//8.0+,不设置这个flag可能会报错
            params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            params.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        params.format= PixelFormat.RGBA_4444;//设置背景图片
        //Don't set the preview visibility to GONE or INVISIBLE
        mWindowManager.addView(mMonitorView, params);
    }

    private void hideWindow() {
        if(null != mWindowManager) {
            mWindowManager.removeView(mMonitorView);
        }
    }
}
